/*
 * Copyright (c) 2013 The CCP project authors. All Rights Reserved. Use of this source code is governed by a Beijing
 * Speedtong Information Technology Co.,Ltd license that can be found in the LICENSE file in the root of the web site.
 * http://www.cloopen.com An additional intellectual property rights grant can be found in the file PATENTS. All
 * contributing project authors may be found in the AUTHORS file in the root of the source tree.
 */
package com.bst.bsj.portal.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;

import com.homewaimai.common.lang.StringUtil;

public class DateUtil {

    /**
     * 变量：日期格式化类型 - 格式:yyyy/MM/dd
     * 
     * @since 1.0
     */
    public static final int DEFAULT            = 0;

    /**
     * 变量：日期格式化类型 - 格式:yyyy/MM
     * 
     * @since 1.0
     */
    public static final int YM                 = 1;

    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd
     * 
     * @since 1.0
     */
    public static final int YMR_SLASH          = 11;

    /**
     * 变量：日期格式化类型 - 格式:yyyyMMdd
     * 
     * @since 1.0
     */
    public static final int NO_SLASH           = 2;

    /**
     * 变量：日期格式化类型 - 格式:yyyyMM
     * 
     * @since 1.0
     */
    public static final int YM_NO_SLASH        = 3;

    /**
     * 变量：日期格式化类型 - 格式:yyyy/MM/dd HH:mm:ss
     * 
     * @since 1.0
     */
    public static final int DATE_TIME          = 4;

    /**
     * 变量：日期格式化类型 - 格式:yyyyMMddHHmmss
     * 
     * @since 1.0
     */
    public static final int DATE_TIME_NO_SLASH = 5;

    /**
     * 变量：日期格式化类型 - 格式:yyyy/MM/dd HH:mm
     * 
     * @since 1.0
     */
    public static final int DATE_HM            = 6;

    /**
     * 变量：日期格式化类型 - 格式:HH:mm:ss
     * 
     * @since 1.0
     */
    public static final int TIME               = 7;

    /**
     * 变量：日期格式化类型 - 格式:HH:mm
     * 
     * @since 1.0
     */
    public static final int HM                 = 8;

    /**
     * 变量：日期格式化类型 - 格式:HHmmss
     * 
     * @since 1.0
     */
    public static final int LONG_TIME          = 9;
    /**
     * 变量：日期格式化类型 - 格式:HHmm
     * 
     * @since 1.0
     */

    public static final int SHORT_TIME         = 10;

    /**
     * 变量：日期格式化类型 - 格式:yyyy-MM-dd HH:mm:ss
     * 
     * @since 1.0
     */
    public static final int DATE_TIME_LINE     = 12;

    public static String dateToStr(Date date, String pattern) {
        if (date == null || date.equals("")) return null;
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static boolean betweenTime(Date startTime, Date endTime, Date currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2000, Calendar.JANUARY, 1, startTime.getHours(), startTime.getMinutes(), startTime.getSeconds());
        long start = calendar.getTimeInMillis();
        calendar.set(2000, Calendar.JANUARY, 1, endTime.getHours(), endTime.getMinutes(), endTime.getSeconds());
        long end = calendar.getTimeInMillis();
        calendar.set(2000, Calendar.JANUARY, 1, currentTime.getHours(), currentTime.getMinutes(),
                     currentTime.getSeconds());
        long current = calendar.getTimeInMillis();
        if (current >= start && current <= end) {
            return true;
        }
        return false;
    }

    public static Date parseEnDate(String dateStr, String timeFormate) {
        SimpleDateFormat timeFormater = new SimpleDateFormat(timeFormate, Locale.ENGLISH);
        try {
            if (StringUtil.isNotBlank(dateStr)) {
                return timeFormater.parse(dateStr);
            }
        } catch (Exception e) {
        }
        return null;
    }

    public static String dateToStr(Date date) {
        return dateToStr(date, "yyyy/MM/dd");
    }

    public static String dateToStr(Date date, int type) {
        switch (type) {
            case DEFAULT:
                return dateToStr(date);
            case YM:
                return dateToStr(date, "yyyy/MM");
            case NO_SLASH:
                return dateToStr(date, "yyyyMMdd");
            case YMR_SLASH:
                return dateToStr(date, "yyyy-MM-dd");
            case YM_NO_SLASH:
                return dateToStr(date, "yyyyMM");
            case DATE_TIME:
                return dateToStr(date, "yyyy/MM/dd HH:mm:ss");
            case DATE_TIME_NO_SLASH:
                return dateToStr(date, "yyyyMMddHHmmss");
            case DATE_HM:
                return dateToStr(date, "yyyy/MM/dd HH:mm");
            case TIME:
                return dateToStr(date, "HH:mm:ss");
            case HM:
                return dateToStr(date, "HH:mm");
            case LONG_TIME:
                return dateToStr(date, "HHmmss");
            case SHORT_TIME:
                return dateToStr(date, "HHmm");
            case DATE_TIME_LINE:
                return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
            default:
                throw new IllegalArgumentException("Type undefined : " + type);
        }
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.betweenTime(parseEnDate("2015-08-11 17:44:40", "yyyy-MM-dd HH:mm:ss"),
                                                parseEnDate("2015-03-11 18:44:40", "yyyy-MM-dd HH:mm:ss"), new Date()));
    }
}
